package com.gis3c.ol.web.controller;

import com.gis3c.ol.entity.Layer;
import com.gis3c.ol.entity.Map;
import com.gis3c.ol.entity.Source;
import com.gis3c.ol.service.LayerService;
import com.gis3c.ol.service.MapService;
import com.gis3c.ol.service.SourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * Created by hukekuan on 2018/3/14.
 */
@RestController
@RequestMapping(value = "/api/ol")
public class OlController {
    @Autowired
    private MapService mapService;
    @Autowired
    private LayerService layerService;
    @Autowired
    private SourceService sourceService;

    @RequestMapping(value = "/map/mapinit")
    public Object APIMapInit(@RequestParam(value = "mapid") String mapId){
        java.util.Map<String,Object> result = new HashMap<>();
        Map queryMap = mapService.findMapById(mapId);
        String[] layerIdArray;
        if(queryMap != null){
            result.put("map",queryMap);
            layerIdArray = queryMap.getLayers();
            result.put("layer",layerService.findLayerByIds(layerIdArray));
        }
        return result;
    }

    @RequestMapping(value = "/map/initview")
    public Object findMapInitiallyView(@RequestParam(value = "mapid") String mapId){
        Map queryMap = mapService.findMapById(mapId);
        return queryMap != null ? queryMap.getView():null;
    }


    @RequestMapping(value = "/layer/layerentry",method = RequestMethod.GET)
    @ResponseBody
    public Object findLayerById(@RequestParam(value = "layername", required = true) String layerName){
        java.util.Map<String,Object> result = new HashMap<>();
        Layer queryLayer = layerService.findeLayerByName(layerName);
        String sourceId = queryLayer == null ? "":queryLayer.getSource();
        result.put("layer",queryLayer);
        if(sourceId != null && !"".equals(sourceId)){
            result.put("source",sourceService.findSourceById(sourceId));
        }

        return result;
    }

    @RequestMapping(value = "/layer/layeroptions",method = RequestMethod.GET)
    @ResponseBody
    public Object findLayerOptionsByName(@RequestParam(value = "layername", required = true) String layerName){
        Layer queryLayer = layerService.findeLayerByName(layerName);
        java.util.Map<String,Object> layerOptions = queryLayer.getOptions();
        if(layerOptions != null){
            if(layerOptions.keySet().contains("styleSet")){
                layerOptions.remove("styleSet");
            }
            if(layerOptions.keySet().contains("successCallBack")){
                layerOptions.remove("successCallBack");
            }
            if(layerOptions.keySet().contains("errorCallBack")){
                layerOptions.remove("errorCallBack");
            }
        }
        return layerOptions;
    }

    @RequestMapping(value = "/source/sourceurl",method = RequestMethod.GET)
    public Object findSourceUrlByName(@RequestParam(value = "sourcename", required = true) String sourceName){
        String result = null;
        Source querySource = sourceService.findSourceByName(sourceName);
        java.util.Map<String,Object> sourceOptions = querySource.getOptions();
        if(sourceOptions.keySet().contains("url")){
            result = sourceOptions.get("url").toString();
        }else {
            result = "";
        }
        return result;
    }
}
